package phone.qyg.com.qyg.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.app.application.QXApplication;
import com.app.base.tool.BaseFragment;
import com.app.config.ApplicationConfig;
import com.app.utils.JSONTool;
import com.app.utils.PromptManager;
import com.app.utils.TransformController;
import com.app.xutils.MyCallBack;
import com.app.xutils.Xutils;
import com.liaoinstan.springview.container.AliFooter;
import com.liaoinstan.springview.container.AliHeader;
import com.liaoinstan.springview.widget.SpringView;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.activity.HtmlActivity;
import phone.qyg.com.qyg.adapter.CulturalMarkAdapter;
import phone.qyg.com.qyg.model.Culture;

public class CulturalThreeFragment extends BaseFragment {
    @ViewInject(R.id.gridview)
    GridView gridview;
    @ViewInject(R.id.springview)
    SpringView springview;
    CulturalMarkAdapter adapter;
    List<Culture> list = new ArrayList<>();
    int pageNo = 1;
    int totalpage=1;
    @Override
    public View initView(LayoutInflater inflater) {
        View view = View.inflate(QXApplication.getContext(), R.layout.fragment_cultural, null);
        x.view().inject(this, view);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        springview.setHeader(new AliHeader(QXApplication.getContext(),false));
        springview.setFooter(new AliFooter(QXApplication.getContext(),false));
        adapter = new CulturalMarkAdapter(getActivity(),list);
        gridview.setAdapter(adapter);
        getInitNetwork();
        setListener();

    }

    private void setListener() {
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Culture item = adapter.getItem(position);
                TransformController.transformControllerModel(QXApplication.getContext(), HtmlActivity.class,item);
            }
        });
        springview.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                pageNo=1;
                getInitNetwork();
            }

            @Override
            public void onLoadmore() {
                if(pageNo>=totalpage){
                    PromptManager.showToast(R.string.more_no_datas);
                    springview.onFinishFreshAndLoad();
                    return;
                }
                pageNo++;
                getInitNetwork();
            }
        });
    }

    @Override
    public void onNetChange(int netMobile) {

    }

    /**
     * 执行网络请求
     */
    private void getInitNetwork() {
        HashMap<String,String> mapKeys = new HashMap<String,String>();
        mapKeys.put("cate_id", "93");
        mapKeys.put("pageNo",pageNo+"");

        Xutils.Get(ApplicationConfig.NET_DSN_PATH + ApplicationConfig.TRES_INFO_LIST,
                mapKeys,
                new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);

                        List<Culture> temps = JSONTool.jsonDefaluTranClazzs(result, JSONTool.Enum.RES_INFO_LIST, Culture.class);

                        totalpage = JSONTool.requestJSONfindNameCount(result, JSONTool.Enum.DATA, JSONTool.Enum.TOTALPAGE);

                        Message message = new Message();
                        message.obj = temps;
                        message.what = 0;
                        mHandler.sendMessage(message);
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        PromptManager.showToast(R.string.network_error);
                        springview.onFinishFreshAndLoad();
                    }

                    @Override
                    public void onFinished() {
                        super.onFinished();
                        springview.onFinishFreshAndLoad();
                    }
                });
    }
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            List<Culture> venues = (List<Culture>) msg.obj;
            switch (msg.what){
                case 0:
                    if(pageNo==1){
                        if(venues==null||venues.size()==0){
                            PromptManager.showToast(R.string.empty_data);
                            return;
                        }
                        list.clear();
                    }else{
                        if(venues==null||venues.size()==0){
                            PromptManager.showToast(R.string.more_no_datas);
                            return;
                        }
                    }
                    list.addAll(venues);
                    adapter.notifyDataSetChanged();
                    springview.onFinishFreshAndLoad();
                    break;
            }
        }
    };

}
